#pragma once

#include <iostream>
#include "Protocol.hpp"

enum
{
    Div_Zero = 1,
    Mod_Zero,
    Other_Oper
};

class ServerCal
{
public:
    ServerCal()
    {
        ;
    }

    Response CalculatorHelper(const Request &req)
    {
        Response resp(0, 0);
        switch (req._op)
        {
        case '+':
            resp._result = req._left + req._right;
            break;
        case '-':
            resp._result = req._left - req._right;
            break;
        case '*':
            resp._result = req._left * req._right;
            break;
        case '/':
        {
            if (req._right == 0)
                resp._code = Div_Zero;
            else
                resp._result = req._left / req._right;
        }
        break;
        case '%':
        {
            if (req._right == 0)
                resp._code = Mod_Zero;
            else
                resp._result = req._left % req._right;
        }
        break;
        default:
            resp._code = Other_Oper;
            break;
        }

        return resp;
    }

    // "len"\n"10 + 20"\n
    std::string Calculator(std::string &package)
    {
        string content;
        bool res = Decode(package, &content);
        if (!res)
        {
            return "";
        }

        Request rst;
        res = rst.Deserialize(content);
        if (!res)
        {
            return "";
        }

        Response rse = CalculatorHelper(rst);
        string rse_content;
        bool rse_res = rse.Serialize(&rse_content);
        if (!rse_res)
        {
            return "";
        }

        rse_content = Encode(rse_content);

        return rse_content;
    }
    ~ServerCal()
    {
    }
};